package com.wf.course.web.alioss;

import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;

/**
 * Created by yanpanpan<yanpanpan@1rock.net> on 2017/7/20
 */

public class UploadPartProgressListener implements ProgressListener {

    private int partNumber;

    private static  long bytesWritten = 0;
    private static long totalBytes = -1;
    private boolean succeed = false;
    private static double percent  = 0.0;

    public UploadPartProgressListener(int partNumber) {
        this.partNumber = partNumber;
    }


    @Override
    public void progressChanged(ProgressEvent progressEvent) {
        long bytes = progressEvent.getBytes();
        ProgressEventType eventType = progressEvent.getEventType();
        switch (eventType) {
            case TRANSFER_PART_STARTED_EVENT:
                System.out.println("Start to upload part #" + this.partNumber + " ......");
                break;

            case REQUEST_CONTENT_LENGTH_EVENT:
                this.totalBytes = bytes;
                System.out.println("part #" + this.partNumber + " with " + this.totalBytes + " bytes will be uploaded to OSS");
                break;

            case REQUEST_BYTE_TRANSFER_EVENT:
                this.bytesWritten += bytes;
                if (this.totalBytes != -1) {
                    int percent = (int)(this.bytesWritten * 100.0 / this.totalBytes);
                    System.out.println(bytes + " bytes have been written at this time, part #" + this.partNumber +
                            "upload progress: " + percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")");
                } else {
                    System.out.println(bytes + " bytes have been written at this time, part #" + this.partNumber +
                            "upload ratio: unknown(" + this.bytesWritten + "/...)");
                }
                break;

            case TRANSFER_PART_COMPLETED_EVENT:
                this.succeed = true;
                System.out.println("Succeed to upload part #" + this.partNumber +", " + this.bytesWritten +
                        " bytes have been transferred in total");
                break;

            case TRANSFER_PART_FAILED_EVENT:
                System.out.println("Failed to upload part #" + this.partNumber +", " + this.bytesWritten +
                        " bytes have been transferred");
                break;

            default:
                break;
        }
    }

    public boolean isSucceed() {
        return succeed;
    }


    public static  double getPercent(Object file) throws IOException {
        InputStream content  = null;

        if(file!=null) {
            if (file instanceof CommonsMultipartFile) {
                CommonsMultipartFile file1 = (CommonsMultipartFile) file;
                // 获取指定文件的输入流
                content = file1.getInputStream();
            } else if (file instanceof MultipartFile) {
                MultipartFile file2 = (MultipartFile) file;
                // 获取指定文件的输入流
                content = file2.getInputStream();
            } else if (file instanceof File) {
                File file3 = (File) file;
                // 获取指定文件的输入流
                content = new FileInputStream(file3);
            }

            totalBytes = Long.valueOf(content.available());

            if (totalBytes != 0) {
                if (percent < 100.0) {
                    percent = (bytesWritten * 100 / totalBytes);
                    System.out.println("上传进度：" + percent + "%");

                }else if(percent >= 100.0){
                    bytesWritten = 0;
                    percent=0;
                }
            }
        }
            return percent;
        }

    }
